using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ODT.Common.Core;
using PPPlotControl.Properties;

namespace PPPlotControl
{
	public class SetArrayIndex : Form
	{
		public enum DialogType
		{
			ARRAYIndex,
			COORDinate
		}

		private readonly ILanguageTranslationService translationService;

		private readonly string invalidIndexText;

		private IContainer components;

		private Label label1;

		private TextBox IndexTextBox;

		private Label labelError;

		private Label label2;

		private Label label3;

		public int Index { get; private set; }

		public List<int> MultIndices { get; }

		public DialogType Dtype { get; set; }

		public SetArrayIndex(ILanguageTranslationService translationService, string translationKey)
		{
			Dtype = DialogType.ARRAYIndex;
			Index = -1;
			MultIndices = new List<int>();
			InitializeComponent();
			base.Icon = Resources.ApplicationIcon;
			base.ShowIcon = true;
			this.translationService = translationService;
			invalidIndexText = this.translationService.Translate("PlotControl.InvalidIndexText");
			label1.Text = this.translationService.Translate(translationKey);
			label2.Text = translationService.Translate("PlotControl.AddMultipleIndicesMessage");
			label3.Text = translationService.Translate("PlotControl.AddRangeIndicesMessage");
			Text = translationService.Translate("PlotControl.SetArrayIndexText");
		}

		public SetArrayIndex()
		{
			InitializeComponent();
			base.Icon = Resources.ApplicationIcon;
			base.ShowIcon = true;
		}

		public void SetFormName(string Name)
		{
			Text = Name;
		}

		private void function(object sender, EventArgs e)
		{
			try
			{
				Index = -1;
				Index = Convert.ToInt32(IndexTextBox.Text);
				labelError.Text = string.Empty;
				Close();
				base.DialogResult = DialogResult.OK;
			}
			catch
			{
				labelError.Text = invalidIndexText;
			}
		}

		private void IndexTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\r')
			{
				return;
			}
			try
			{
				Index = -1;
				if (Dtype != DialogType.COORDinate)
				{
					string[] array = IndexTextBox.Text.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
					for (int i = 0; i < array.Length; i++)
					{
						string[] array2 = array[i].Split(new char[1] { '-' }, StringSplitOptions.RemoveEmptyEntries);
						int num = Convert.ToInt32(array2[array2.Length - 1]);
						for (int j = Convert.ToInt32(array2[0]); j <= num; j++)
						{
							MultIndices.Add(j);
						}
					}
				}
				else
				{
					Index = Convert.ToInt32(IndexTextBox.Text);
					if (Dtype == DialogType.COORDinate && !checkCoordValidity(Index))
					{
						labelError.Text = invalidIndexText;
						IndexTextBox.Clear();
						return;
					}
				}
				labelError.Text = string.Empty;
				Close();
				base.DialogResult = DialogResult.OK;
			}
			catch
			{
				labelError.Text = invalidIndexText;
				IndexTextBox.Clear();
			}
		}

		private void IndexTextBox_Leave(object sender, EventArgs e)
		{
			try
			{
				Index = -1;
				if (Dtype != DialogType.COORDinate)
				{
					string[] array = IndexTextBox.Text.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
					for (int i = 0; i < array.Length; i++)
					{
						string[] array2 = array[i].Split(new char[1] { '-' }, StringSplitOptions.RemoveEmptyEntries);
						int num = Convert.ToInt32(array2[array2.Length - 1]);
						for (int j = Convert.ToInt32(array2[0]); j <= num; j++)
						{
							MultIndices.Add(j);
						}
					}
				}
				else
				{
					Index = Convert.ToInt32(IndexTextBox.Text);
					if (Dtype == DialogType.COORDinate && !checkCoordValidity(Index))
					{
						labelError.Text = invalidIndexText;
						IndexTextBox.Clear();
						return;
					}
				}
				labelError.Text = string.Empty;
				Close();
				base.DialogResult = DialogResult.OK;
			}
			catch
			{
				labelError.Text = invalidIndexText;
				IndexTextBox.Clear();
			}
		}

		private bool checkCoordValidity(int num)
		{
			if (num > 16)
			{
				return num >= 0;
			}
			return true;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.IndexTextBox = new System.Windows.Forms.TextBox();
			this.labelError = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			base.SuspendLayout();
			this.label1.AutoEllipsis = true;
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(12, 9);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(168, 13);
			this.label1.TabIndex = 0;
			this.label1.Text = "Please enter an index for the array";
			this.IndexTextBox.Location = new System.Drawing.Point(209, 6);
			this.IndexTextBox.Name = "IndexTextBox";
			this.IndexTextBox.Size = new System.Drawing.Size(44, 20);
			this.IndexTextBox.TabIndex = 1;
			this.IndexTextBox.Leave += new System.EventHandler(IndexTextBox_Leave);
			this.IndexTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(IndexTextBox_KeyPress);
			this.labelError.AutoSize = true;
			this.labelError.Location = new System.Drawing.Point(12, 71);
			this.labelError.Name = "labelError";
			this.labelError.Size = new System.Drawing.Size(0, 13);
			this.labelError.TabIndex = 2;
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(12, 38);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(278, 13);
			this.label2.TabIndex = 3;
			this.label2.Text = "To add multiple indices separate the values with commas.\r\n";
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(12, 71);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(292, 13);
			this.label3.TabIndex = 4;
			this.label3.Text = "To add a range of indices separate the values with hyphens.";
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			base.ClientSize = new System.Drawing.Size(363, 126);
			base.Controls.Add(this.label3);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.labelError);
			base.Controls.Add(this.IndexTextBox);
			base.Controls.Add(this.label1);
			base.Name = "SetArrayIndex";
			base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "SetArrayIndex";
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
